//
//  LoginViewController.m
//  HWZB
//
//  Created by 许成雄 on 2017/4/22.
//  Copyright © 2017年 wonders. All rights reserved.
//

#import "LoginViewController.h"
#import "ForgetPasswordViewController.h"
#import "RegisterViewController.h"

#import "NSString+Utils.h"
#import "RegexKit.h"
#import "MBProgressHUD+Toast.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "HWZBAPIRequest.h"
#import "Context.h"

@interface LoginViewController ()

@property (strong, nonatomic) UITextField *usernameTextField;
@property (strong, nonatomic) UITextField *passwordTextField;
@property (strong, nonatomic) UIButton *forgetPasswordButton;
@property (strong, nonatomic) UIButton *loginButton;
@property (strong, nonatomic) UIButton *registerButton;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = I_COLOR_WHITE;
    self.navigationItem.title = @"登录";
    [self setBackNavgationItem];
    
    [self createUI];
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)popBack {
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - UIButton Action
- (void)forgetPasswordButtonAction:(id)sender {
    ForgetPasswordViewController *forgetPasswordViewController = [[ForgetPasswordViewController alloc] init];
    [self.navigationController pushViewController:forgetPasswordViewController animated:YES];
}

- (void)loginButtonAction:(id)sender {
    NSString *username = self.usernameTextField.text;
    NSString *password = self.passwordTextField.text;
    if([NSString isBlankString:username]) {
        [MBProgressHUD showToast:@"请输入电话号码" initWithView:self.view];
        return;
    } else if(![RegexKit validateMobile:username]) {
        [MBProgressHUD showToast:@"请输入正确的电话号码" initWithView:self.view];
        return;
    }
    if([NSString isBlankString:password]) {
        [MBProgressHUD showToast:@"请输入密码" initWithView:self.view];
        return;
    } else if(password.length < 6 || password.length > 18) {
        [MBProgressHUD showToast:@"密码过短，正常密码长度6~18位" initWithView:self.view];
        return;
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    [params setObject:username forKey:@"mobile"];
    [params setObject:password forKey:@"password"];
    [SVProgressHUD showWithStatus:@"正在登录..." maskType:SVProgressHUDMaskTypeClear];
    [[HWZBAPIRequest sharedInstance] userLoginWithParams:params success:^(LoginResultInfo *loginResultInfo) {
        [SVProgressHUD dismiss];
        if(loginResultInfo != nil) {
            [Context sharedInstance].loginID = loginResultInfo.userId;
            [Context sharedInstance].mobile = loginResultInfo.mobile;
            [MBProgressHUD showToast:@"登录成功" initWithView:self.view];
            [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationUserLogin object:nil];
            [self performSelector:@selector(dismissThisViewController) withObject:nil afterDelay:1.5f];
        } else {
            [MBProgressHUD showToast:@"登录失败, 请检查网络" initWithView:self.view];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
        [MBProgressHUD showToast:error.localizedDescription initWithView:self.view];
    }];
}

- (void)dismissThisViewController {
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
    }];
}

- (void)registerButtonAction:(id)sender {
    RegisterViewController *registerViewController = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:registerViewController animated:YES];
}

#pragma mark - Private Method
- (void)createUI {
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT- 64.0f - TRANS_VALUE(98.0f))];
    bgImageView.contentMode = UIViewContentModeScaleToFill;
    bgImageView.image = [UIImage imageNamed:@"bg_login"];
    [self.view addSubview:bgImageView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(60.0f), SCREEN_WIDTH, TRANS_VALUE(30.0f))];
    titleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(15.0f)];
    titleLabel.textColor = I_COLOR_WHITE;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"登录";
    [bgImageView addSubview:titleLabel];
    
    UIView *usernameView = [[UIView alloc] initWithFrame:CGRectMake(TRANS_VALUE(30.0f), TRANS_VALUE(110.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(30.0f), TRANS_VALUE(40.0f))];
    usernameView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.3f];
    [self.view addSubview:usernameView];
    
    UIView *imgUsernameBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, TRANS_VALUE(40.0f), TRANS_VALUE(40.0f))];
    imgUsernameBg.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.2f];
    UIImageView *usernameImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(12.5f), TRANS_VALUE(12.0f), TRANS_VALUE(15.0f), TRANS_VALUE(16.0f))];
    usernameImageView.contentMode = UIViewContentModeCenter;
    usernameImageView.image = [UIImage imageNamed:@"ic_login_username"];
    [imgUsernameBg addSubview:usernameImageView];
    [usernameView addSubview:imgUsernameBg];
    
    self.usernameTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(46.0f), TRANS_VALUE(0.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(30.0f) - TRANS_VALUE(52.0f), TRANS_VALUE(40.0f))];
    self.usernameTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(14.0f)];
    self.usernameTextField.textColor = I_COLOR_WHITE;
    self.usernameTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入手机号" attributes:@{NSForegroundColorAttributeName:I_COLOR_LIGHT_WHITE}];
    self.usernameTextField.tintColor = I_COLOR_WHITE;
    [usernameView addSubview:self.usernameTextField];
    self.usernameTextField.keyboardType = UIKeyboardTypeNumberPad;
    
    UIView *passwordView = [[UIView alloc] initWithFrame:CGRectMake(TRANS_VALUE(30.0f), TRANS_VALUE(154.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(30.0f), TRANS_VALUE(40.0f))];
    passwordView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.3f];
    [self.view addSubview:passwordView];
    
    UIView *imgPasswordBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, TRANS_VALUE(40.0f), TRANS_VALUE(40.0f))];
    imgPasswordBg.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.2f];
    UIImageView *passwordImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(13.0f), TRANS_VALUE(8.5f), TRANS_VALUE(14.0f), TRANS_VALUE(23.0f))];
    passwordImageView.contentMode = UIViewContentModeCenter;
    passwordImageView.image = [UIImage imageNamed:@"ic_login_password"];
    [imgPasswordBg addSubview:passwordImageView];
    [passwordView addSubview:imgPasswordBg];
    
    self.passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(46.0f), TRANS_VALUE(0.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(30.0f) - TRANS_VALUE(52.0f), TRANS_VALUE(40.0f))];
    self.passwordTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(14.0f)];
    self.passwordTextField.textColor = I_COLOR_WHITE;
    self.passwordTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入密码" attributes:@{NSForegroundColorAttributeName:I_COLOR_LIGHT_WHITE}];
    self.passwordTextField.tintColor = I_COLOR_WHITE;
    [self.passwordTextField setSecureTextEntry:YES];
    [passwordView addSubview:self.passwordTextField];
    
    self.forgetPasswordButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - TRANS_VALUE(110.0f), TRANS_VALUE(194.0f), TRANS_VALUE(80.0f), TRANS_VALUE(30.0f))];
    [self.forgetPasswordButton setBackgroundColor:[UIColor colorWithWhite:1.0f alpha:0.0f]];
    [self.forgetPasswordButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    self.forgetPasswordButton.titleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(12.0f)];
    [self.forgetPasswordButton setImage:[UIImage imageNamed:@"ic_login_forget_password"] forState:UIControlStateNormal];
    [self.forgetPasswordButton setImage:[UIImage imageNamed:@"ic_login_forget_password"] forState:UIControlStateSelected];
    [self.forgetPasswordButton setImage:[UIImage imageNamed:@"ic_login_forget_password"] forState:UIControlStateHighlighted];
    [self.forgetPasswordButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, TRANS_VALUE(4.0f))];
    [self.forgetPasswordButton setTitleColor:I_COLOR_WHITE forState:UIControlStateNormal];
    [self.forgetPasswordButton setTitleColor:I_COLOR_WHITE forState:UIControlStateSelected];
    [self.forgetPasswordButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    [self.forgetPasswordButton setTitle:@"忘记密码" forState:UIControlStateSelected];
    [self.view addSubview:self.forgetPasswordButton];
    [self.forgetPasswordButton addTarget:self action:@selector(forgetPasswordButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.loginButton = [[UIButton alloc] initWithFrame:CGRectMake(TRANS_VALUE(30.0f), TRANS_VALUE(240.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(30.0f), TRANS_VALUE(40.0f))];
    [self.loginButton setBackgroundColor:[UIColor colorWithWhite:1.0f alpha:0.8]];
    self.loginButton.titleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(14.0f)];
    [self.loginButton setTitleColor:I_COLOR_RED forState:UIControlStateNormal];
    [self.loginButton setTitleColor:I_COLOR_RED forState:UIControlStateNormal];
    [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [self.view addSubview:self.loginButton];
    [self.loginButton addTarget:self action:@selector(loginButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.registerButton = [[UIButton alloc] initWithFrame:CGRectMake(TRANS_VALUE(30.0f), TRANS_VALUE(284.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(30.0f), TRANS_VALUE(40.0f))];
    [self.registerButton setBackgroundColor:[UIColor colorWithWhite:1.0f alpha:0.4]];
    self.registerButton.titleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(14.0f)];
    [self.registerButton setTitleColor:I_COLOR_WHITE forState:UIControlStateNormal];
    [self.registerButton setTitleColor:I_COLOR_WHITE forState:UIControlStateSelected];
    [self.registerButton setTitle:@"快速注册" forState:UIControlStateNormal];
    [self.registerButton setTitle:@"快速注册" forState:UIControlStateSelected];
    [self.view addSubview:self.registerButton];
    [self.registerButton addTarget:self action:@selector(registerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *logoImageView = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - TRANS_VALUE(208)) / 2, SCREEN_HEIGHT- 64.0f - TRANS_VALUE(78.0f), TRANS_VALUE(208.0f), TRANS_VALUE(58.0f))];
    logoImageView.contentMode = UIViewContentModeScaleAspectFit;
    logoImageView.image = [UIImage imageNamed:@"ic_login_logo"];
    [self.view addSubview:logoImageView];
    
}

- (void)loadData {
    //TODO -- 测试账号
//    self.usernameTextField.text = @"18616625906";
//    self.passwordTextField.text = @"111111";
    self.usernameTextField.text = @"18049882220";
    self.passwordTextField.text = @"771230";
}


@end
